<?php


namespace app\appa\controller;


use app\appa\model\AppraModel;
use app\appa\model\MasterModel;
use app\appa\model\MembersModel;
use app\appa\validate\AppraValidate;
use app\common\controller\Api;
use think\Request;

class Apa extends Api
{
    public function add(Request $request)
    {
        $data = input('post.');
        //验证数据
        $validate = new AppraValidate();
        $result = $validate->scene('add')->check($data);
        if (!$result){
            $this->error($validate->getError());
        }
        $order = MembersModel::find($request->userId);
        $data['members_id'] = $request->userId;
        $data['members_name'] = $order['name'];
        $data['image'] = $order['image'];
        $data['create_time'] = date('Y-m-d H:i:s');
        if ($data['assess'] >= 4){
            $master = MasterModel::find($data['master_id']);
            $master -> praise = $master['praise'] +1;
            if ($master ->save()){
                if (AppraModel::create($data)){
                    $this->success('添加成功');
                }
                $this->error('添加失败');
            }
        }
        if (AppraModel::create($data)){
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    public function index1(Request $request)
    {
        $master_id = input('master_id');
        if (empty($master_id)){
            $this->error('未确认查看哪一位师傅的ID');
        }
        $order =  AppraModel::order('id','DESC')
            ->field('members_name,image,create_time,text,images,assess')
            ->where('master_id',$master_id)
            ->where('assess', '>=',4)
            ->select();
        if (!$order){
            $this->error('该师傅暂无评价');
        }
        $this->success('查询成功',$order);
    }

    public function index2(Request $request)
    {
        $master_id = input('master_id');
        if (empty($master_id)){
            $this->error('未确认查看哪一位师傅的ID');
        }
        $order =  AppraModel::order('id','DESC')
            ->field('members_name,image,create_time,text,images,assess')
            ->where('master_id',$master_id)
            ->where('assess', '=',3)
            ->select();
        if (!$order){
            $this->error('该师傅暂无评价');
        }
        $this->success('查询成功',$order);
    }

    public function index3(Request $request)
    {
        $master_id = input('master_id');
        if (empty($master_id)){
            $this->error('未确认查看哪一位师傅的ID');
        }
        $order =  AppraModel::order('id','DESC')
            ->field('members_name,image,create_time,text,images,assess')
            ->where('master_id',$master_id)
            ->where('assess', '<=',2)
            ->select();
        if (!$order){
            $this->error('该师傅暂无评价');
        }
        $this->success('查询成功',$order);
    }

}